class Queue {
    constructor() {
      this.queue = [];
    }
  
    write(data) {
      this.queue.push(data); // 向队列末尾添加新数据
    }
  
    read() {
      return this.queue.shift(); // 从队列头部取出数据
    }
  }
  
  class Producer {
    constructor() {}
  
    create(data) {
      log('+ prd create:', data);
      queue.write(data);
    }
  }
  
  class Consumer {
    constructor() {}
  
    destroy() {
      const data = queue.read();
      if (data !== undefined) {
        log('- csm destroy:', data);
      } else {
        log('- csm destroy: queue is empty');
      }
    }
  }
  
  // 全局变量 queue，模拟队列
  const queue = new Queue();
  
  function log(...args) {
    console.log(...args);
  }
  
  function main() {
    const prd = new Producer();
    const csm = new Consumer();
  
    for (let i = 0; i < 5; i++) {
      setTimeout(() => prd.create(i), i * 100); // 每隔 100ms 创建数据
      setTimeout(() => csm.destroy(), i * 100 + 50); // 每隔 100ms 之后的50ms消费数据
    }
  }
  
  main();